#define DEBUG
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 1000000;

int min(int a, int b) {
  return a<b ? a : b;
}

int max(int a, int b) {
  return a>b ? a : b;
}

int main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  static int a[maxn+1];
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    scanf("%d", a+i);
  }

  long long ans=0;
  for (int i=2; i<n; i++) {
    if (a[i]>a[i-1] && a[i]>a[i+1]) {
      ans += a[i]-max(a[i+1], a[i-1]);
      a[i] = max(a[i+1], a[i-1]);
    }
    if (a[i]<a[i-1] && a[i]<a[i+1]) {
      ans += min(a[i+1], a[i-1])-a[i];
      a[i] = min(a[i+1], a[i-1]);
    }
  }
  for (int i=2; i<=n; i++) {
    ans += abs(a[i]-a[i-1]);
  }
  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
